//
//  UserAccount.swift
//  WeiBo
//
//  Created by 小小罗 on 15/11/9.
//  Copyright © 2015年 RealMadrid. All rights reserved.
//

import UIKit

//用户账户模型
class UserAccount: NSObject,NSCoding {

    
    // 用于调用access_token，接口获取授权后的access token
    var access_token: String?
//    access_token的生命周期，单位是秒数
    var expires_in:NSTimeInterval = 0 {
        didSet{

//            一旦从服务器获得过期的时间，立刻计算准确的日期
            expiresDate = NSDate(timeIntervalSinceNow: expires_in)
        }
    
    }
    
    //过期日期
    var expiresDate: NSDate?
    
    // 当前授权用户的UID
    var uid: String?
    
    /// 用户昵称
    var screen_name: String?
    /// 用户头像地址（大图），180×180像素
    var avatar_large: String?
    
    init(dict:[String: AnyObject]) {
        super.init()
        
        setValuesForKeysWithDictionary(dict)
    
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    override var description: String {
        
        let keys = ["access_token", "expires_in","uid","expiresDate","screen_name","avatar_large"]
        
        return dictionaryWithValuesForKeys(keys).description
    }
    
       
    
   // MARK: - `键值`归档和解档
    //归档 -在把对象保存到磁盘前  将对象编码成二进制数据 跟网络的序列化概念很像
    func encodeWithCoder(aCoder: NSCoder) {
//        Option-左箭头
//        将文本插入点移至上一个单词的的开头
        aCoder.encodeObject(access_token,forKey: "access_token")
        aCoder.encodeObject(expiresDate,forKey: "expiresDate")
        aCoder.encodeObject(uid,forKey: "uid")
        aCoder.encodeObject(screen_name,forKey: "screen_name")
        aCoder.encodeObject(avatar_large,forKey: "avatar_large")
    }
    
    
    //解档:- 从磁盘加载二进制文件，转换成对象时调用 － 跟网络的反序列化很像
    required init?(coder aDecoder: NSCoder) {
        
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
        uid = aDecoder.decodeObjectForKey("uid") as? String
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
    
    }
    
    
    

    
}

// 在 extension 中只允许写 便利构造函数，而不能写指定构造函数
// 不能定义存储型属性，定义存储型属性，会破坏类本身的结构！ 所以不能在这里边归档解档
extension UserAccount {
    
    //    required init?(coder aDecoder: NSCoder) {
    //
    //        access_token = aDecoder.decodeObjectForKey("access_token") as? String
    //        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
    //        uid = aDecoder.decodeObjectForKey("uid") as? String
    //        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
    //        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
    //    }
}





